package com.xhl.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.xhl.service.OrderItemService;
import com.xhl.service.UserService;
import com.xhl.util.JwtUtil;
import com.xhl.util.SpringContextUtil;
import com.xhl.vo.Pager;
import com.xhl.vo.ResultVO;
import com.xhl.vo.UserVO;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    OrderItemService orderItemService;

    @PostMapping(value = "/verify", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String userVerify(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("userId");
        String name = request.getParameter("userName");
        String password = request.getParameter("userPassWord");
        @SuppressWarnings("unchecked")
        ResultVO<UserVO> rvo = SpringContextUtil.getBean(ResultVO.class);
        if (id == null || name == null || password == null) {    //信息不全 没有登陆
            rvo.setCode("101");
            rvo.setMsg("没有登陆,请登陆");
            System.out.println(rvo);
            String json = JSON.toJSONString(rvo);
            return json;
        }

        String token = request.getHeader("Authorization");
        Long l = JwtUtil.verify(token);

        if (l == 0) {        // token 失效重新登陆
            rvo.setCode("101");
            rvo.setMsg("登陆失效,请重新登陆");
            System.out.println(rvo);
            String json = JSON.toJSONString(rvo);
            return json;
        }


        UserVO uvo = SpringContextUtil.getBean(UserVO.class);
        uvo.setId(Integer.valueOf(id));
        uvo.setName(name);
        uvo.setPassword(password);

        UserVO userInfo = userService.queryByUserPass(uvo);

        if (userInfo == null) {    // 密码错误
            rvo.setCode("101");
            rvo.setMsg("登陆失效,已修改密码");
            System.out.println(rvo);
            String json = JSON.toJSONString(rvo);
            return json;
        }


        System.out.println("登陆的信息为：   " + userInfo);
        rvo.setCode("200");
        rvo.setMsg("登陆成功");
        rvo.setBody(userInfo);
        String json = JSON.toJSONString(rvo);
        return json;
    }


    @GetMapping(value = "/nubmer", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String nubmerOrder(HttpServletRequest request, HttpServletResponse response) {
        String uid = request.getParameter("uid");
        if (uid == null) {
            return "异常";
        }

        Long nubmer = orderItemService.queryOrderItem(uid);
        if (nubmer == null) {
            nubmer = (long) 0;
        }
        String json = JSON.toJSONString(nubmer);
        return json;
    }


    @PostMapping(value = "/login", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String loginUser(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        if (name == null) {
            return "异常";
        }
        @SuppressWarnings("unchecked")
        ResultVO<UserVO> rvo = SpringContextUtil.getBean(ResultVO.class);

        Long countUser = userService.queryByUserName(name);

        if (countUser == 0) {
            rvo.setCode("300");
            rvo.setMsg("请输入正确账号: " + name);
            String json = JSON.toJSONString(rvo);
            return json;
        }
        UserVO uvo = SpringContextUtil.getBean(UserVO.class);
        uvo.setName(name);
        uvo.setPassword(password);
        UserVO userInfo = userService.queryByUserPass(uvo);
        System.out.println(userInfo);
        if (userInfo == null) {
            rvo.setCode("300");
            rvo.setMsg("请输入正确密码: " + password);
            String json = JSON.toJSONString(rvo);
            return json;
        }


        String token = JwtUtil.sign(userInfo.getId());
        rvo.setCode("200");
        rvo.setMsg(token);
        rvo.setBody(userInfo);
        String json = JSON.toJSONString(rvo);
        return json;
    }


    @PostMapping(value = "/add", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String addUser(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String pass = request.getParameter("pass");
        System.out.println(name + "  " + pass);
        if (name == null) {
            return "异常";
        }
        Long countUser = userService.queryByUserName(name);
        System.out.println(countUser);
        if (countUser != 0) {
            return "false";
        }
        UserVO uvo = SpringContextUtil.getBean(UserVO.class);
        uvo.setName(name);
        uvo.setPassword(pass);
        userService.addUser(uvo);
        return "true";
    }

    @RequestMapping("/query")
    @ResponseBody
    public String queryStu(HttpServletRequest request, HttpServletResponse response) {

        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");

        if (pageNum == null) {
            return "异常";
        }
        @SuppressWarnings("unchecked")
        Pager<UserVO> p = SpringContextUtil.getBean(Pager.class);
        //
        p.setSize(Integer.valueOf(pageSize));
        p.setPage((Integer.valueOf(pageNum) - 1) * Integer.valueOf(pageSize));

        long count = userService.count();
        List<UserVO> arr = userService.queryUser(p);
        p.setTotal(count);
        p.setRows(arr);
        String json = JSON.toJSONString(p);
        return json;
    }
}
